<template>
  <div class="change-password-wrapper">
    <el-card class="change-password-card">
      <h3 class="title">个人资料修改</h3>
      <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          class="change-password-form"
          label-width="100px"
      >
        <el-form-item label="姓名" prop="adName">
          <el-input v-model="form.adName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
              v-model="form.newPassword"
              type="password"
              placeholder="留空则不修改"
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
              v-model="form.confirmPassword"
              type="password"
              placeholder="与新密码保持一致"
          />
        </el-form-item>
        <div class="button-item">
          <el-button
              class="submit-button"
              type="primary"
              @click="onSubmit"
          >
            保存
          </el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getCurrentUser, updateAdmin } from '@/api/apiList/admin'
import { adminName } from '@/store/user'
const router   = useRouter()
const formRef  = ref(null)
const form     = reactive({
  adId: null,
  adAccount: '',
  adName: '',
  phone: '',
  newPassword: '',
  confirmPassword: ''
})
const rules = {
  adName: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入电话', trigger: 'blur' },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: '手机号格式不正确',
      trigger: 'blur'
    }
  ],
  newPassword: [
    { min: 6, message: '密码长度至少 6 位', trigger: 'blur' }
  ],
  confirmPassword: [
    {
      validator: (rule, value) => {
        if (form.newPassword && value !== form.newPassword) {
          return new Error('两次密码必须一致')
        }
        return true
      },
      trigger: 'blur'
    }
  ]
}

// 初始化
onMounted(async () => {
  try {
    const admin = await getCurrentUser()
    form.adId   = admin.adId
    form.adName = admin.adName
    form.adAccount = admin.account
    form.phone  = admin.phone
  } catch {
    ElMessage.error('获取用户信息失败，请重新登录')
    router.replace({ name: 'Login' })
  }
})

async function onSubmit() {
  // 校验
  await formRef.value.validate(async valid => {
    if (!valid) return
    // 组装参数
    const payload = {
      adId: form.adId,
      adName: form.adName,
      adAccount: form.phone,
      phone: form.phone
    }
    if (form.newPassword) {
      payload.adPassword = form.newPassword
    }
    try {
      await updateAdmin(payload)
      ElMessage.success('修改成功')
      localStorage.setItem('adminName', form.adName)
      adminName.value = form.adName
      router.go(0)   // 整页刷新
      router.back()
    } catch {
      ElMessage.error('保存失败，请稍后重试')
    }
  })
}
</script>

<style scoped>
.change-password-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 40px;
}
.change-password-card {
  width: 400px;
  padding: 20px;
}
.title {
  text-align: center;
  margin-bottom: 40px;
  color: #333;
}
.change-password-form {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.el-form-item {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}
.el-form-item__label {
  width: 100px;
  text-align: right;
  padding-right: 10px;
}
.el-form-item__content {
  width: 260px;
}
.button-item {
  margin-top: 20px;
  text-align: center;
}
.submit-button {
  width: 100px;
}
</style>
